package com.itheima.mm.service.impl;


import com.itheima.mm.dao.QuestionDao;
import com.itheima.mm.dao.TagDao;
import com.itheima.mm.dao.UserDao;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.pojo.Tag;
import com.itheima.mm.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagDao tagDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private QuestionDao questionDao;

    @Override
    public List<Tag> findListByPage(QueryPageBean queryPageBean) {
        List<Tag> tagList=tagDao.findListByPage(queryPageBean);
        for (Tag tag : tagList) {
            tag = replenishTag(tag);
        }

        return tagList;
    }

    @Override
    public Long findTotal(QueryPageBean queryPageBean) {
        return tagDao.findTotal(queryPageBean);
    }

    @Override
    public void delete(Integer tagId) {
        tagDao.deleteQusTag(tagId);
        tagDao.delete(tagId);
    }

    @Override
    public void add(Tag tag) {
        tagDao.add(tag);
    }

    private Tag replenishTag (Tag tag){
        tag.setCreator(userDao.findById(tag.getUserId()).getUsername());
        tag.setQuestionQty(questionDao.findQuestionQtyByTypeId(tag.getId()));
        return tag;
    }
}
